<template>
    <div class="imgForm">

        <CountDown v-if="this.Filesuccess" url = "/imgDown" ></CountDown>

        <MainForm :formData = this.formImgData>
            <div slot="title" class="title">留下你的足迹</div>

         
               <div class="form-item" slot="file">
                    <label for="name">选择分享的图片(最佳尺寸215*230)：</label>
                    <input type="file" name="file" accept="image/jpg,image/jpeg,image/png,image/PNG" ref="imgfile" @change="changepic($event)">
                    <!--当vaule值改变时执行changepic函数，规定上传的文件只能是图片-->
                </div>

                 <div class="form-item" slot="longText">
                    <label for="name">留言内容:</label>
                    <textarea rows="8" cols="30" name="longText" v-model="formImgData.longText">
                    </textarea>
                </div>

                <div class="form-item" slot="button">
                    <button @click.once="mySubmit">提交</button>
                </div>
        </MainForm>

    </div>
</template>

<script>
import MainForm from '@/components/common/formCpn/mainForm'
import CountDown from '@/components/common/countDowm/countDown'//跳转成功提示组件

import {imgDown} from '@/network/imgUpload/imgDown'

export default {
    name:'imgForm',
    components:{
        MainForm,
        CountDown
    },
    data(){
        return{
            formImgData:{
                imgFile:'',
                longText:'',
                isDef:false
            },
            Filesuccess: false,
            
            
        }
    },
    methods:{
        changepic(e){
            let imgFile = this.$refs.imgfile.files[0]
            //console.log(param.get('imgFile')) // FormData私有类对象，访问不到，可以通过get判断值是否传进去
            if(imgFile.size > 2097152){
                alert('上传图片不得大于2MB')
				//结构赋值
                this.$refs.imgfile.outerHTML=this.$refs.imgfile.outerHTML
            }else{
                this.formImgData.isDef = true
            }
            
        },

        mySubmit(){
            let param = new FormData(); //创建form对象
            this.formImgData.imgFile = this.$refs.imgfile.files[0]
            param.append('imgFile', this.formImgData.imgFile) // 通过append向form对象添加数据
            param.append('longText', this.formImgData.longText)

            //console.log(param.get('imgFile'))
            if(this.formImgData.isDef === true){
                imgDown(param).then( res =>{
                    //console.log(this.formImgData)
                    this.Filesuccess = true
                    //console.log(res)
                }).catch(err => {
                    console.log(err)
                })
            }
        }

    }

    
}
</script>

<style scoped>
.imgForm{
    position: fixed;
    width: 100%;
    height: 100%;
    background: url(~@/assets/img/common/form/ad.jpg);
}
</style>